package de.widas.gaesf.main;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import de.widas.gaesf.dataobject.Eintrag;
import de.widas.gaesf.service.EintragManager;

/**
 * Created on: 24.09.2009, 16:05:02<br/>
 * Copyright 2009 by WidasConcepts GmbH<br/>
 * 
 * @author Meykel Gruel
 */
public class MainPage extends WebPage {

	private String eingabe = "Please insert Comment here";
	private String autor = "Autor...";

	@SpringBean
	private EintragManager service;

	public MainPage() {
		this.setOutputMarkupId(true);
		
		IModel<List<Eintrag>> lModel = new AbstractReadOnlyModel<List<Eintrag>>() {
			private static final long serialVersionUID = -7247111396742582697L;

			@Override
			public List<Eintrag> getObject() {
				return service.getObjects();
			}
		};

		final ListView<Eintrag> lView = new ListView<Eintrag>("eintragList", lModel) {

			private static final long serialVersionUID = 2027684351856126565L;

			@Override
			protected void populateItem(ListItem<Eintrag> pListItem) {
				pListItem.add(new Label("listEintragText", new PropertyModel<String>(pListItem.getModelObject(), "text")));
				pListItem.add(new Label("listEintragAutor", new PropertyModel<String>(pListItem.getModelObject(), "autor")));
			}
		};
		lView.setOutputMarkupId(true);
		
		add(lView);
		
		Form<Void> lForm = new Form<Void>("editEintragForm");
		final TextField<String> lTxtEingabe = new TextField<String>("editEintragText", new PropertyModel<String>(this, "eingabe"));
		lForm.add(lTxtEingabe);
		final TextField<String> lTxtAutor = new TextField<String>("editEintragAutor", new PropertyModel<String>(this, "autor"));
		lForm.add(lTxtAutor);
		AjaxButton lBtn = new AjaxButton("eintragSave") {

			private static final long serialVersionUID = -3897978322285249957L;

			@Override
			protected void onSubmit(AjaxRequestTarget pTarget, Form<?> pForm) {
				Eintrag lObj = new Eintrag();
				lObj.setText(eingabe);
				lObj.setAutor(autor);
				service.save(lObj);
				eingabe = "Please insert Comment here";
				autor = "Author...";

				pTarget.addComponent(MainPage.this);
			}
		};
		lForm.add(lBtn);
		lForm.setOutputMarkupId(true);
		
		add(lForm);
	}
}
